﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using Runda.CMS.Common.Utility;
using System.Collections.Generic;
using System.Text;

namespace Runda.CMS.Portal.SalesManage
{
    public partial class Delbill : BasePage
    {
        public string BillYear
        {
            get
            {
                if (Request.QueryString["billYear"] != null) return Request.QueryString["billYear"].ToString();
                else return "0000";
            }

        }

        public string BillMonth
        {
            get
            {
                if (Request.QueryString["billMonth"] != null) return Request.QueryString["billMonth"].ToString();
                else return "00";
            }

        }

        public string company
        {
            get
            {
                if (Request.QueryString["company"] != null) return Request.QueryString["company"].ToString();
                else return "";
            }

        }

        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            if (txtbillno.Text.Trim() == "") { return; }
            string sb = getWhereClause();
            int rows;
            DataTable dt = new BillBll().GetSalesBills(sb, "instrumentName");
            rows = dt.Rows.Count;

            gridAccessoryList.DataSource = dt;
            gridAccessoryList.DataBind();

            if (rows > 0) { btnDel.Enabled = true; }
            else { btnDel.Enabled = false; }
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            int delrows;
            if (txtbillno.Text.Trim() != "")
            {
                delrows = new BillBll().DelSalesBill(txtbillno.Text.Trim());
                if (delrows > 0)
                { 
                    ClientScript.RegisterStartupScript(this.GetType(), "saveandssscloseerror", "alert('发票被成功删除!');", true);
                    string sb = getWhereClause();
                    int rows;
                    DataTable dt = new BillBll().GetSalesBills(sb, "instrumentName");
                    rows = dt.Rows.Count;

                    gridAccessoryList.DataSource = dt;
                    gridAccessoryList.DataBind();

                    if (rows > 0) { btnDel.Enabled = true; }
                    else { btnDel.Enabled = false; }
                }
                else
                { ClientScript.RegisterStartupScript(this.GetType(), "saveandssscloseerror", "alert('发票删除失败!');", true); }

            }
        }

        private string getWhereClause()
        {
            StringBuilder sb = new StringBuilder();
            string beginDT, endDT;

            beginDT = BillYear + "-" + BillMonth + "-01";
            endDT = DataValid.Monthlastday(BillYear + "-" + BillMonth + "-01");

            sb.Append(" and a.[dDate]  between '").Append(beginDT + "' and '").Append(endDT + "'");

            if (company != "")
            {
                sb.Append(" and BillUnit='").Append(company).Append("'");
            }
            sb.Append(" and cBVCode='").Append(txtbillno.Text.Trim()).Append("'");
            


            return sb.ToString();
        }
        
        
    }
}
